//
//  MainTabBar.m
//  MyMicroBlog
//
//  Created by Apple on 16/9/21.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "MainTabBar.h"
#import "UIView+Extension.h"
@interface MainTabBar()
@property(strong,nonatomic)UIButton *button;
@end

@implementation MainTabBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

//懒加载
-(UIButton *)button{
    if (!_button) {
        _button = [[UIButton alloc]init];
        //按钮图片
        [_button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        //按钮背景
        [_button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [_button sizeToFit];
        //点击事件
        [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

#pragma mark -搭建界面
-(void)setupUI{
    //去除黑边
    self.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    [self addSubview:self.button];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _button.centerX = self.width / 2;
    _button.centerY = self.height / 2;
    //计算每个按钮的长度
    CGFloat itemWidth = self.width / 5;
    //遍历子控件,记录下标
    __block NSInteger index = 0;
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            obj.width = itemWidth;
            obj.x = index * itemWidth;
            index += 1;
            //第三个按钮多偏移一个按钮宽度
            if (index == 2) {
                index += 1;
            }
        }
    }];
}

//点击事件
-(void)buttonClick{
    self.composeButtonAcition();
}

@end
